package com.cf.base.sp;


import android.content.Context;

import com.cf.base.sp.builder.IBuilder;
import com.cf.base.sp.builder.IReadBuilder;
import com.cf.base.sp.engine.Converter;
import com.cf.base.sp.engine.Encryption;
import com.cf.base.sp.engine.JsonEngine;
import com.cf.base.sp.engine.Log;
import com.cf.base.sp.engine.Serializer;
import com.cf.base.sp.engine.Storage;
import com.cf.base.sp.engine.impl.DefaultConverterImpl;
import com.cf.base.sp.engine.impl.DefaultEncryptionImpl;
import com.cf.base.sp.engine.impl.DefaultSerializerImpl;
import com.cf.base.sp.engine.impl.FastJsonEngineImpl;
import com.cf.base.sp.engine.impl.LogImpl;
import com.cf.base.sp.engine.impl.MemoryStorageImpl;
import com.cf.base.sp.engine.impl.SharedPreferencesStorageImpl;

/**
 * SpCore 核心配置
 *
 * @author yangji
 */
public class SpCoreBuilder implements IBuilder, IReadBuilder {

    private static final String DEFAULT_SP_CORE_TAG = "DEFAULT_SP_CORE_TAG";
    private Context mContext;
    private Storage mStorage;
    private Converter mConverter;
    private Serializer mSerializer;
    private Encryption mEncryption;
    private Log mLog;
    private String mTag = DEFAULT_SP_CORE_TAG;
    private JsonEngine mJson;

    SpCoreBuilder(Context context) {
        if (context != null) {
            this.mContext = context.getApplicationContext();
        }
    }

    @Override
    public void build() {
        SpCore.build(this);
    }

    @Override
    public IBuilder setTag(String tag) {
        this.mTag = tag;
        return this;
    }

    @Override
    public IBuilder setStorage(Storage storage) {
        this.mStorage = storage;
        return this;
    }

    @Override
    public IBuilder setIJson(JsonEngine json) {
        this.mJson = json;
        return this;
    }

    @Override
    public IBuilder setConverter(Converter converter) {
        this.mConverter = converter;
        return this;
    }

    @Override
    public IBuilder setSerializer(Serializer serializer) {
        this.mSerializer = serializer;
        return this;
    }

    @Override
    public IBuilder setEncryption(Encryption encryption) {
        this.mEncryption = encryption;
        return this;
    }

    @Override
    public IBuilder setLog(Log log) {
        mLog = log;
        return this;
    }

    @Override
    public Storage getStorage() {
        if (mStorage == null) {
            if (mContext == null) {
                mStorage = new MemoryStorageImpl();
            } else {
                mStorage = new SharedPreferencesStorageImpl(mContext, mTag);
            }
        }
        return mStorage;
    }

    @Override
    public JsonEngine getJson() {
        mJson = mJson == null ? new FastJsonEngineImpl() : mJson;
        return mJson;
    }

    @Override
    public Converter getConverter() {
        mConverter = mConverter == null ? new DefaultConverterImpl(getJson()) : mConverter;
        return mConverter;
    }

    @Override
    public Serializer getSerializer() {
        mSerializer = mSerializer == null ? new DefaultSerializerImpl() : mSerializer;
        return mSerializer;
    }

    @Override
    public Encryption getEncryption() {
        mEncryption = mEncryption == null ? new DefaultEncryptionImpl() : mEncryption;
        return mEncryption;
    }

    @Override
    public Log getLog() {
        mLog = mLog == null ? new LogImpl() : mLog;
        return mLog;
    }
}
